﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace net.xBei.Helper
{
    /// <summary>
    /// 字符串相关
    /// </summary>
    public static class StringHelper
    {
        /// <summary>
        /// 掩码
        /// </summary>
        /// <param name="s"></param>
        /// <param name="begin">0开始</param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Mask(this string s, int begin, int length) {
            return $"{s[..begin]}{"".PadLeft(length, '*')}{s[^(begin + length)..]}";
        }
        /// <summary>
        /// 如果<paramref name="input"/>为空则返回<paramref name="dv"/>
        /// </summary>
        /// <param name="input"></param>
        /// <param name="dv"></param>
        /// <returns></returns>
        public static string Get(this string input, string dv) => string.IsNullOrWhiteSpace(input) ? dv : input;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="body"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string GetMessage(this ReadOnlyMemory<byte> body, Encoding encoding) => encoding.GetString(body.ToArray());
        /// <summary>
        /// 
        /// </summary>
        /// <param name="body"></param>
        /// <param name="encodingName"></param>
        /// <returns></returns>
        public static string GetMessage(this ReadOnlyMemory<byte> body, string encodingName = "utf-8") => Encoding.GetEncoding(encodingName).GetString(body.ToArray());
        /// <summary>
        /// 使用UTF-8编码
        /// </summary>
        /// <param name="body"></param>
        /// <returns></returns>
        public static string GetMessage(this ReadOnlyMemory<byte> body) => Encoding.UTF8.GetString(body.ToArray());
        /// <summary>
        /// 使用UTF-8编码
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="body"></param>
        /// <param name="dv"></param>
        /// <returns></returns>
        public static T? GetMessage<T>(this ReadOnlyMemory<byte> body, T? dv = default) => GetMessage(body).TryDeserialize(dv);
        /// <summary>
        /// 判断字符串是否以列表中的任意一个字符串结尾，忽略大小写
        /// </summary>
        /// <param name="str"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static bool EndsWithAny(this string str, IEnumerable<string> list)
            => list.Any(s => str.EndsWith(s, StringComparison.OrdinalIgnoreCase));

        #region 各种数据验证
        /// <summary>
        /// 是否邮件地址
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static bool VerifyEmail(this string email) {
            var pattern = @"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$";
            return Regex.IsMatch(email, pattern);
        }
        /// <summary>
        /// 是否手机号码（中国）
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public static bool VerifyMobile(this string mobile) {
            //var pattern = @"^1[3|5|7|8|]\d{9}$";
            var pattern = @"^\d{11}$";
            return Regex.IsMatch(mobile, pattern);
        }
        /// <summary>
        /// 验证密码（简单字符）
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static bool VerifyPassword(this string password) {
            foreach (var p in password) {
                var intp = (int)p;
                if (intp <= 0 || intp > 127)
                    return false;
            }
            return true;
        }
        #endregion
    }
}
